﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace StateMachineXNA
{
    class StatePauseMenu : IGameState
    {
        Color clerp(Color a, Color b, float v)
        {
            return new Color(a.ToVector4() * v + b.ToVector4() * (1.0f - v));
        }

        private StateGame game;
        private SpriteFont font;
        private Texture2D tex;
        private Timer timer;

        public StatePauseMenu(StateGame game)
        {
            this.game = game;
        }

        public void Init(Game1 game)
        {
            if (font == null) font = game.Content.Load<SpriteFont>("Font");

            tex = new Texture2D(game.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            tex.SetData<Color>(new Color[] { Color.White });

            timer = new Timer(Math.PI);
        }

        public void Pause()
        { }

        public void Resume()
        { }

        public void Update(StateMachine<IGameState> sm, GameTime gt)
        {
            if (Input.KeyPressed(Keys.Escape))
                sm.Pop();

            if (timer.Increment((float)gt.ElapsedGameTime.TotalSeconds * 2))
                timer.Restart(true);
        }

        public void Draw(SpriteBatch sb)
        {
            game.Draw(sb);

            // Tint the screen
            sb.Draw(tex, new Rectangle(0, 0, 1000, 1000), Color.White * 0.25f);

            sb.DrawString(
                font,
                "You're now paused", // Text
                new Vector2(100, 200), // Position
                clerp( // Color interpolation
                    Color.White * 0.0f,
                    Color.White,
                    (float)Math.Sin(timer.ElapsedTime)
                )
            );
        }
    }
}
